package com.ubing.boot.notesforjava.controller;

import com.ubing.boot.notesforjava.domel.entity.Image;
import com.ubing.boot.notesforjava.domel.entity.UserInfo;
import com.ubing.boot.notesforjava.response.Response;
import com.ubing.boot.notesforjava.service.IUserInfoService;
import com.ubing.boot.notesforjava.utils.FileDownloadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ubing
 * @since 2023-02-17
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
    //注入service
    @Autowired
    IUserInfoService iUserInfoService;
    // 检查token
    @RequestMapping("/checkToken")
    public Response checkToken(HttpServletRequest request){ return iUserInfoService.checkToken(request); }

    // 用户登录，返回token
    @RequestMapping("/login")
    public Response login(@RequestBody UserInfo userInfo){
        return iUserInfoService.login(userInfo);
    }

    // 用户注册，返回
    @RequestMapping("/register")
    public Response register(@RequestBody UserInfo userInfo){
        return iUserInfoService.register(userInfo);
    }


    //用户头像上传
    @RequestMapping("/avatarUpload")
    public Response avatarUpload(MultipartFile file, String name) throws IOException, InterruptedException {
//        MultipartFile file = image.getFile();
        System.out.println(name);
//        System.out.println(image.getFile());
//        System.out.println(image.getWidth());
//        System.out.println(image.getHeight());

        return iUserInfoService.avatarUpload(file,name);
    }

    //校验用户名是否重复
    @RequestMapping("/validateUserName")
    public Response validateUserName(@RequestBody UserInfo userInfo){
        return iUserInfoService.validateUserName(userInfo);
    }


    @RequestMapping("/test")
    public Response test(@RequestBody MultipartFile file, String test) throws IOException {

        String avatarName = FileDownloadUtils.downAvatar(file, "baba.png");
        System.out.println(file);
        return Response.success(test);
    }
}

